package com.fishery.customization.controller;

import com.fishery.customization.entity.vo.*;
import com.fishery.customization.service.FixedLabelService;
import com.fishery.customization.service.LabelService;
import com.fishery.entity.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/customization/label")
@Api(tags = "标签管理模块")
@CrossOrigin
public class LabelController {

	@Autowired
	private LabelService labelService;

	@Autowired
	private FixedLabelService fixedLabelService;


	@PostMapping
	@ApiOperation("查询用户的标签")
	public Result searchLabelsOfUser(@RequestBody @ApiParam(value = "searchLabelCondition")
											 SearchLabelCondition condition) {
		if (condition == null
				|| StringUtils.isEmpty(condition.getBaseId())
				|| StringUtils.isEmpty(condition.getRole())
				|| StringUtils.isEmpty(condition.getUserId())) {
			return Result.error("查询失败");
		}
		return Result.success().data(labelService
				.getLabelsJsonByBaseIdAndRoleAndUserId(condition.getBaseId(),
						condition.getRole(), condition.getUserId()));
	}

	@PostMapping("/generate")
	@ApiOperation("选择基地类型并生成标签（前端不需要调用，提供给远程调用）")
	public Result generateLabels(@RequestBody @ApiParam(value = "baseTypeVo") BaseTypeVo baseTypeVo) {
		if (baseTypeVo == null
				|| StringUtils.isEmpty(baseTypeVo)
				|| baseTypeVo.getTypes() == null
				|| baseTypeVo.getTypes().isEmpty()) {
			return Result.error("生成标签失败");
		}
		return labelService.createLabelsOfBase(baseTypeVo.getBaseId(), baseTypeVo.getTypes())
				? Result.success("生成标签成功") : Result.error("生成标签失败");
	}

	@PostMapping("/role/generate")
	@ApiOperation("基地创建角色需要调取的接口")
	public Result generateLabelsForRoleOfBase(@RequestBody BaseRoleLabelVo vo) {
		if (vo == null
				|| StringUtils.isEmpty(vo.getBaseId())
				|| StringUtils.isEmpty(vo.getRole())) {
			return Result.error();
		}
		return labelService.createLabelsForRoleOfBase(vo.getBaseId(), vo.getRole())
				? Result.success() : Result.error();
	}

	@GetMapping("/type/{baseId}")
	@ApiOperation("查询基地类型")
	public Result searchTypesOfBase(@PathVariable(name = "baseId", required = true)
									@ApiParam(value = "基地id", required = true) String baseId) {
		if (StringUtils.isEmpty(baseId)) {
			return Result.error("查询失败");
		}
		return Result.success().data(labelService.getTypesOfBase(baseId));
	}

	@PutMapping("/extension")
	@ApiOperation("拓展基地类型")
	public Result extendLabels(@RequestBody @ApiParam(value = "baseTypeVo") BaseTypeVo baseTypeVo) {
		if (baseTypeVo == null
				|| StringUtils.isEmpty(baseTypeVo)
				|| baseTypeVo.getTypes() == null
				|| baseTypeVo.getTypes().isEmpty()) {
			return Result.error("拓展失败");
		}
		return labelService.extendLabelsOfBase(baseTypeVo.getBaseId(), baseTypeVo.getTypes())
				? Result.success("拓展标签成功") : Result.error("拓展标签失败");
	}

	@PutMapping("/role/name")
	@ApiOperation("更新基地角色名")
	public Result updateNameOfBaseRole(@RequestBody @ApiParam(value = "updateRoleNameVo") UpdateRoleNameVo vo) {
		if (vo == null
				|| StringUtils.isEmpty(vo.getBaseId())
				|| StringUtils.isEmpty(vo.getOldName())
				|| StringUtils.isEmpty(vo.getNewName())) {
			return Result.error("更新失败");
		}
		return labelService.updateNameOfBaseRoleByBaseId(vo.getBaseId(), vo.getOldName(), vo.getNewName())
				? Result.success("更新成功") : Result.error("更新失败");
	}

	@PutMapping("/role/function")
	@ApiOperation("更新基地角色权限时需要调用")
	public Result updateLabelsForRoleOfBase(@RequestBody UpdateRoleLabelVo updateRoleLabelVo) {
		if (updateRoleLabelVo == null
				|| StringUtils.isEmpty(updateRoleLabelVo.getBaseId())
				|| StringUtils.isEmpty(updateRoleLabelVo.getRole())
				|| updateRoleLabelVo.getFunctionMap() == null
				|| updateRoleLabelVo.getFunctionMap().isEmpty()) {
			return Result.error("更新失败");
		}
		return labelService.updateLabelsForRoleOfBase(updateRoleLabelVo.getBaseId(),
				updateRoleLabelVo.getRole(), updateRoleLabelVo.getFunctionMap()) ?
				Result.success("更新成功") : Result.error("更新失败");
	}

	@GetMapping("/type/all")
	@ApiOperation("查询所有的基地类型")
	public Result searchAllTypesOfBase() {
		return Result.success().data(fixedLabelService.getAllTypesOfBase());
	}

}
